<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" ></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'diaobo.delete'" ></el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.return_code" @keyup.enter="upsearch" placeholder="退货单号"> </el-input>
                    <el-select v-model="search.status" placeholder="审核状态" clearable>
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                        <el-option label="退回" :value="2"></el-option>
                    </el-select>
                    <el-date-picker v-model="search.return_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border>
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <!-- <el-table-column type="index" width="50" align="center"></el-table-column> -->
                <el-table-column label="退货单号" prop="return_code">
                    <template #default="{row}">
                        <el-link :underline="false" type="primary" style="font-size:12px" @click="table_show(row)">{{ row.return_code }}</el-link>
                        <div style="margin: 5px 0">{{row.return_date}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="类型" prop="input_type">
                    <template #default="{row}">
                        {{row.input_type == 1 ? '供应商' : row.input_type == 2 ? '染色厂' : '加工厂'}}
                    </template>
                </el-table-column>
                <el-table-column label="退货数量(匹)" prop="total_count" align="center" width="120"></el-table-column>
                <el-table-column label="审核" prop="status" width="100" align="center">
                    <template #default="{row}">
                        <el-button :type="row.status == 0 ? 'warning' : row.status == 1 ? 'primary' : 'danger'" size="small" @click="purchase_check(row)" v-auth="'diaobo.check'">{{row.status == 0 ? '待审' : row.status == 1 ? '通过' : '退回'}}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="文件" prop="upload" width="100" align="center">
                    <template #default="{row}">
                        <el-button type="primary" size="small" @click="purchase_upload(row)" v-if="row.status == 1" v-auth="'diaobo.upload'">查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark"></el-table-column>
                <el-table-column label="业务员" prop="admin.name"></el-table-column>
                <el-table-column label="操作" align="center" width="150">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-button type="primary" size="small" @click="table_edit(scope.row)" v-auth="'diaobo.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'diaobo.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
    <upload-dialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload = false"></upload-dialog>
    
</template>

<script>
import saveDialog from './save'

import CheckStatus from '../../checkStatus.vue'
import uploadDialog from '../../upload.vue'
export default {
    name: '采购计划管理',
    components: {
        saveDialog, CheckStatus, uploadDialog
    },
    data() {
        return {
            dialog: {
                save: false,
                check: false,
                upload: false,
            },
            apiObj: this.$API.business.return.list,
            selection: [],
            search: {
                purchase_code: null,
                ware_date: [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
                status: null,
                goods_name: null
            },
        }
    },
    mounted() {
    },
    methods: {
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('show').setData(row)
            })
        },
        //删除
        async table_del(row) {
            var reqData = { id: row.id }
            var res = await this.$API.business.return.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        async table_del_all(delete_ids) {
            var reqData = { id: delete_ids }
            var res = await this.$API.business.return.delete.post(reqData);
            if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("删除成功")
            } else {
                this.$alert(res.message, "提示", { type: 'error' })
            }
        },
        //批量删除
        async batch_del() {
            var delete_ids = [];
            this.selection.forEach((item) => {
                delete_ids.push(item.id)
            })

            this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
                type: 'warning'
            }).then(() => {
                this.table_del_all(delete_ids)
            }).catch(() => {

            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            } else if (mode == 'return') {
                this.$refs.table.refresh()
            }
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.search.ware_date = [this.moment().subtract(30, 'days').format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
            this.$refs.table.upData(this.search)
        },
        // 调拨审核
        purchase_check(row){
            this.dialog.check = true
            this.$nextTick(() => {
                this.$refs.checkDialog.open('return').setData(row,row.admin?.name)
            })
        },
        // 查看签名
        purchase_upload(row){
            this.dialog.upload = true
            this.$nextTick(() => {
                this.$refs.uploadDialog.open('show').setData(row)
            })
        },
    }
}
</script>

<style></style>
